<template>
  <div>
    <el-table :data="tableData.data" style="width: 100%">
      <el-table-column label="投保人头像" width="150">
        <template slot-scope="scope">
          <span style="margin-left: 10px">
            <img v-if="scope.row.img" width="50px" :src="'http://localhost:3000/images/'+scope.row.img" alt />
              <img width="50px" v-else src="http://localhost:3000/images/timg.jpg" alt />

          </span>
       
        </template>
      </el-table-column>
      <el-table-column label="投保人" width="110">
        <template slot-scope="scope">
          <span>{{scope.row.username}}</span>
        </template>
      </el-table-column>
      <el-table-column label="产品名称" width="110">
        <template slot-scope="scope">
          <span>关联产品id</span>
        </template>
      </el-table-column>
      <el-table-column label="所属销售员" width="110">
        <template slot-scope="scope">
          <span>{{scope.row.salesmanId.salesName}}</span>
        </template>
      </el-table-column>
      <el-table-column label="投保有效期" width="110">
        <template slot-scope="scope">
          <span>无</span>
        </template>
      </el-table-column>
      <el-table-column label="投保金额" width="110">
        <template slot-scope="scope">
          <span>无</span>
        </template>
      </el-table-column>
      <el-table-column label="投保人电话" width="140">
        <template slot-scope="scope">
          <span>{{scope.row.tel}}</span>
        </template>
      </el-table-column>
      <el-table-column label="状态" width="110">
        <template slot-scope="scope">
          <el-switch v-model="scope.row.state" active-color="#13ce66" inactive-color="#ff4949"></el-switch>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center">
        <template slot-scope="scope">
          <el-button size="mini" @click="handleEdit( scope.row._id)">修改</el-button>
          <!-- 修改对话框 -->
          <el-dialog title="修改投保人信息" :visible.sync="dialogFormVisible">
            <el-form :model="form">
              <el-form-item label="投保人" :label-width="formLabelWidth">
                <el-input v-model="form.username" autocomplete="off"></el-input>
              </el-form-item>
                <el-form-item label="电话" :label-width="formLabelWidth">
                <el-input v-model="form.tel" autocomplete="off"></el-input>
              </el-form-item>

                          </el-form>
            <div slot="footer" class="dialog-footer">
              <el-button @click="dialogFormVisible = false">取 消</el-button>
              <el-button type="primary" @click="confirm">确 定</el-button>
            </div>
          </el-dialog>
          <!-- ----------- -->
          <el-button size="mini" type="danger" @click="handleDelete(scope.$index, scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="tableData.currentPage-0"
      :page-sizes="[3,5,7]"
      :page-size="5"
      layout="total, sizes, prev, pager, next, jumper"
      :total="tableData.total">
    </el-pagination>
  </div>
</template>

<script>
import { createNamespacedHelpers } from "vuex";
let { mapState:mapState1, mapActions:mapActions1 } = createNamespacedHelpers("customer");

export default {
  created() {
    this.render();
  },
  data() {
    return {
      // 数据
      tableData: {},
      dialogFormVisible: false,
      form: {
        username: "",
        tel: ''
      },
      formLabelWidth: "120px",
        pageData :{
           currentPage:1,
           pageSize: 6
        }
    };
  }, 

  methods: {
    ...mapActions1(["renderCustomer", "delCustomer","getOne",'update']),
   
     handleSizeChange(val) {
              this.pageData.pageSize=val;
              this.render();
      },
      handleCurrentChange(val) {
               this.pageData.currentPage=val;
               this.render();
      },
    //  渲染页面
    async render() {
      // 管理员进入渲染所有客户
       let { data } = await this.renderCustomer(this.pageData);
      this.tableData = data;
      console.log(this.pageData)
    },
    
    //修改
   async handleEdit(_id) {
      this.dialogFormVisible = true;
         let data = await this.getOne(_id);
          if(data.status){
             this.form=data.data[0];           
          }
    },
   //  确认修改
   async confirm(){
  this.dialogFormVisible=false;
      let data = this.update(this.form);
      this.render();
      if(data.status){
            this.$message({
            type: "success",
            message: "修改成功!",
          });
      }
   },
    //删除
    async handleDelete(index, row) {
      this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.delCustomer(row._id);
          this.render();
          this.$message({
            type: "success",
            message: "删除成功!",
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
  },
};
</script>

<style>
</style>